package ocm.oh.ds;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Paint;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *  Very simple JFrame which displays an egg-carton 
 */

public class QueenGui extends JFrame {
    private int[][] columns = null;
    private int m;
    private int n;
    private int[] queens = null;
    // This variable is used for indicating which cells have been changed
    // i.e useful to demonstrate how a neighbor was changed...
    private ArrayList<Integer> changedIndexes = null; 

    public static void main(String[] args) {
        
        int[] queens = new int[6];
        queens[0] = 4;
        queens[1] = 2;
        queens[2] = 0;
        queens[3] = 5;
        queens[4] = 3;
        queens[5] = 3;
        
        /*
        int[][] columns = new int[6][6];
        columns[0][4] = 1;
        columns[1][2] = 1;
        columns[2][0] = 1;
        columns[3][5] = 1;
        columns[4][3] = 1;
        columns[5][3] = 1; */
        QueenGui gui = new QueenGui("Test", queens);
    }
    
    
    public QueenGui(String title, int[] queens) {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);     
        setSize(300,300);
        setVisible(true);      
        this.queens = queens;
        setBackground(Color.WHITE);
        JPanel panel = new QueenPanel();
        add(panel);  
        setSize(queens.length*35,queens.length*35); 
    }    

    private class QueenPanel extends JPanel{
        public void paintComponent(Graphics g){
            setBackground(Color.WHITE);                       
            // getViolations(3, 0);
            g.setColor(Color.blue);      
            
            for(int row=0; row < queens.length; row++) {
                g.setColor(Color.blue);
                g.fillOval(queens[row]*30+5, row*30+5,20,20);
                
                for(int column=0; column < queens.length; column++) {
                    int violationsIfMovedHere = getViolationsIfMovedHere(column, row);
                    g.setColor(Color.red);
                    g.drawString(""+ violationsIfMovedHere, column*30+15, row*30+15);  
                }       
            }
            /*
            for(int row=0; row < columns.length; row++) {
                for(int column=0; column < columns[0].length; column++) {
                    if(columns[row][column] == 1) {
                        g.setColor(Color.blue);                        
                        g.fillOval(column*30+5, row*30+5, 20, 20);
                        //g.drawString(""+ (row+1), column*40+20, row*40+20);                        
                    }
                    int violationsIfMovedHere =getViolationsIfMovedHere(column, row);
                        g.setColor(Color.red);
                        g.drawString(""+ violationsIfMovedHere, column*30+15, row*30+15);                                                                   
                }
            }
            */
            
            // Now, find all the nr of violations for each column on each row
            // The number should be the number of violations a queen does by moving to it           
            g.setColor(Color.black);            
            for(int i=0; i <= queens.length; i++) {
                // Draw all vertical lines
                g.drawLine(i*30, 0, i*30, queens.length*30);
            }
            
            for(int i=0; i <= queens.length; i++) {
                // Draw all the horizontal lines
                g.drawLine(0,i*30,queens.length*30,i*30);
            }
            
        }
    }
   
    private int getViolationsIfMovedHere(int x, int queen) {
        int count = 0;
        for(int i=0; i < queens.length; i++) {
            if(i != queen) {
                if(queens[i] == x) count++;
                else if(queens[i] == x + Math.abs(queen-i)) count++;
                else if(queens[i] == x - Math.abs(queen-i)) count++;
                //else if(x - Math.abs(x-queens[i]) == queens[i]) count++;
            }
        }        
        return count;
    }
    
    /*
    private int getViolationsIfMovedHere(int x, int y) {
        int count = 0;
        count += getCount(x, y, 1, 1); // diagonal down-right
        count += getCount(x,y,-1,-1); // diagonal up-left
        
        count += getCount(x, y, 1, -1); // up-right
        count += getCount(x,y,-1,1); // down-left        
        count += getCount(x,y,0,1); // Down
        count += getCount(x,y,0,-1); // Up       
        
        count += getCount(x,y,1,0); // right
        count += getCount(x,y,-1,0); // left
        
        return count;
    }
    */
    
    private void getViolations(int x, int y) {
        System.out.println("Violations for : (" + x + "," + y + ")" + getViolationsIfMovedHere(x, y));
    }
    
    private int getCount(int x,int y, int dX, int dY) {
        int curX = x;
        int curY = y;              
        int count = 0;
       // curX += dX;
       // curY += dY;
        while(curX >= 0 && curX < columns[0].length && curY >= 0 && curY < columns.length) {
            if(columns[curY][curX] == 1 && curY != y) count++;
            // System.out.println("Checking cell: Y: " + curY + " X: " + curX);
            curX += dX;
            curY += dY;
        }
        return count;
    }       
}